/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import DAO.BannerDAO;
import Models.Banner;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Hse7en
 */
@ManagedBean
@RequestScoped
public class BannerController {

    private UploadedFile file;
    private static final int BUFFER_SIZE = 6124;
    List<Banner> LstEnabledBanner = null;
    List<Banner> LstAllBanner = null;

    public BannerController() {
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public List<Banner> getEnabledBanner() {
        LstEnabledBanner = new BannerDAO().getEnabledBanner();
        return LstEnabledBanner;
    }

    public List<Banner> getAllBanner() {
        LstAllBanner = new BannerDAO().getAllBanner();
        return LstAllBanner;
    }

    public void addBanner() {

//        Banner b = (Banner) (FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("bannerBean"));
//        String uploadedFolder = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("uploadFolder");
//
//        ExternalContext extContext = FacesContext.getCurrentInstance().
//                getExternalContext();
//        File result = new File(extContext.getRealPath("//" + uploadedFolder) + "//" + file.getFileName());
//        System.out.println(result.getAbsolutePath());
//
//        try {
//            FileOutputStream fileOutputStream = new FileOutputStream(result);
//
//            byte[] buffer = new byte[BUFFER_SIZE];
//
//            int bulk;
//            InputStream inputStream = file.getInputstream();
//            while (true) {
//                bulk = inputStream.read(buffer);
//                if (bulk < 0) {
//                    break;
//                }
//                fileOutputStream.write(buffer, 0, bulk);
//                fileOutputStream.flush();
//            }
//
//            fileOutputStream.close();
//            inputStream.close();
//
//            b.setImage(uploadedFolder + "/" + file.getFileName());
//
//            FacesMessage msg = new FacesMessage("Succesful",
//                    " is uploaded.");
//            FacesContext.getCurrentInstance().addMessage(null, msg);
//
//        } catch (IOException e) {
//            e.printStackTrace();
//
//            FacesMessage error = new FacesMessage("The files were not uploaded!");
//            FacesContext.getCurrentInstance().addMessage(null, error);
//        }
        Banner b = (Banner) (FacesContext.getCurrentInstance().getExternalContext().
                getRequestMap().get("bannerBean"));
        new BannerDAO().addBanner(b);
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("listBanner.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(BannerController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String editBanner() {
//        Banner b = (Banner) (FacesContext.getCurrentInstance().getExternalContext().
//                getRequestMap().get("bannerBean"));
//        new BannerDAO().addBanner(b.getBanner());
        return "listBanner";
    }

    public void selectedID(int selectedID) {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        session.setAttribute("selectedID", selectedID);
    }

    public String editItem(Banner b) {
        Banner bannerBean = new Banner();
        bannerBean.edit(b);

        return "editBanner";
    }

    public void deleteFeedback() {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        int id = Integer.parseInt(session.getAttribute("selectedID").toString());
        new BannerDAO().deleteBannerByID(id);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Information: ", "Banner has been deleted !"));
    }
}
